@(username: String)

$(function() {
    var WS = window['MozWebSocket'] ? MozWebSocket : WebSocket
 var chatSocket = new WS("@routes.Application.chat(username).webSocketURL(request)")

  

    var receiveEvent = function(event) {
        var data = JSON.parse(event.data)

        // Handle errors
        if(data.error) {
            chatSocket.close()
            $("#onError span").text(data.error)
            $("#onError").show()
            return
        } else if(data.type == "exchangecards") {
        	$("#ramkaWymiany").show();
        }	
        else if(data.type == "message") {
            $("#onChat").show()
        
        
        // Create the message element       
	        var el = $('<div class="message"><p style="font-size:16px"></p></div>')
	        $("p", el).text(data.message)
	        $(el).addClass('me')
	        $('#messages').append(el) 
        } else if (data.type == "cards") {	
        	document.getElementById('k0').setAttribute('style', "background-image:url('/assets/images/"+data.card0+".png');");
        	document.getElementById('k1').setAttribute('style', "background-image:url('/assets/images/"+data.card1+".png');");
        	document.getElementById('k2').setAttribute('style', "background-image:url('/assets/images/"+data.card2+".png');");
        	document.getElementById('k3').setAttribute('style', "background-image:url('/assets/images/"+data.card3+".png');");
        	document.getElementById('k4').setAttribute('style', "background-image:url('/assets/images/"+data.card4+".png');");
        } else if (data.type == "pula") {
        	$("#pula").html(data.pula);
        }  else if (data.type == "cash") {
        	$("#cash").html(data.cash);
        }  else if (data.type == "bet") {
        	$("#lastbet").html(data.bet);
        } else if (data.type == "betoptions") {
        	$("#bettingFrame").show();
        	if(data.bet == "1") {$("#bet").show();} else {$("#bet").hide();}
        	if(data.check == "1") {$("#check").show();} else {$("#check").hide();}
        	if(data.raise == "1") {$("#raise").show();} else {$("#raise").hide();}
        	if(data.call == "1") {$("#call").show();} else {$("#call").hide();}
        	if(data.fold == "1") {$("#fold").show();} else {$("#fold").hide();}
        	if(data.allin == "1") {$("#allin").show();} else {$("#allin").hide();}
        }  else if(data.type == "gameover") {
        	$("#start").show();
        	$("#quit").hide();
        	$("#options").show();
        }	
        
    }



 


    chatSocket.onmessage = receiveEvent
    
    $("#wymien").click(function(){
    	var c0, c1, c2, c3, c4;
        c0 = c1 = c2 = c3 = c4 = 0;
        if (document.getElementById('ck0').checked) {
            c0 = 1;
        }
        if (document.getElementById('ck1').checked) {
            c1 = 1;
        }
        if (document.getElementById('ck2').checked) {
            c2 = 1;
        }
        if (document.getElementById('ck3').checked) {
            c3 = 1;
        }
        if (document.getElementById('ck4').checked) {
            c4 = 1;
        }
            chatSocket.send(JSON.stringify(
                {type: 'cardstoexhange',card0:c0, card1:c1, card2:c2, card3:c3, card4:c4, username: '@username'}
            ))
            $("#ramkaWymiany").hide();
        }
    )
   $("#start").click(function(){
	   
	   chatSocket.send(JSON.stringify(
			   {type: 'start',username: '@username'}
       ))
       $("#start").hide();
       $("#quit").show();
       $("#ramkaPuli").show();
       $("#options").hide();
       
       
   }
   )
   
      $("#fold").click(function(){
	   chatSocket.send(JSON.stringify(
			   {
				   type: 'betting',
				   bettingOptions: "fold",
				   username: '@username'
			   }
       ))
       $("#bettingFrame").hide() 
   }
   )
         $("#check").click(function(){
	   chatSocket.send(JSON.stringify(
			   {
				   type: 'betting',
				   bettingOptions: "check",
				   username: '@username'
			   }
       ))
       $("#bettingFrame").hide()
   }
   )
            $("#call").click(function(){
	   chatSocket.send(JSON.stringify(
			   {
				   type: 'betting',
				   bettingOptions: "call",
				   username: '@username'
			   }
       ))
       $("#bettingFrame").hide()
   }
   )
       $("#allin").click(function(){
    	   chatSocket.send(JSON.stringify(
			   {
				   type: 'betting',
				   bettingOptions: "allin",
				   username: '@username'
			   }
       ))
       $("#bettingFrame").hide()
   }
   )
   
   var typeofbet;
    $("#bet").click(function(){
    	typeofbet = "bet";
        $("#bettingFrame").hide();
        $("#heightOfBet").show();
    }
    )
    $("#raise").click(function(){
    	typeofbet = "raise";
        $("#bettingFrame").hide();
        $("#heightOfBet").show();
    }
    )
    $("#ok").click(function(){
    	   chatSocket.send(JSON.stringify(
			   {
				   type: 'betting',
				   bettingOptions: typeofbet,
				   heightofbet: $("#height").val(),
				   username: '@username'
			   }
       ))
       $("#heightOfBet").hide()
   }
   )
       $("#apply").click(function(){ ///////////////////////////////////apply
    	   chatSocket.send(JSON.stringify(
			   {
				   type: 'options',
				   quantityofbots: $("#quantityofbots").val(),
				   quantityofhumans: $("#quantityofhumans").val(),
				   startcash: $("#startcash").val(),
				   startbuyin: $("#startbuyin").val(),
				   username: '@username'
			   }
       ))
       $("#ramkaOpcji").hide()
   }
   )
          $("#options").click(function(){
    	  
       $("#ramkaOpcji").show()
   }
   )
   
    $("#quit").click(function(){
    	   chatSocket.send(JSON.stringify(
			   {
				   type: 'quit',
				   username: '@username'
			   }
       ))
       $("#start").show()
        $("#quit").hide()
   }
   )
})
